

package yf.mall.service.impl.app;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import yf.mall.entity.custom.CountMap;
import yf.mall.vo.up.AppProductMaterialUp;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.excel.context.AnalysisContext;

import java.io.InputStream;

import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import yf.mall.vo.down.AppProductMaterialDown;

import java.util.Collections;
import java.util.List;
import java.net.URLEncoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.stream.Collectors;

import yf.mall.dao.AppProductMaterialDao;
import yf.mall.entity.AppProductMaterialEntity;
import yf.mall.vo.AppProductMaterialVo;
import yf.mall.service.app.AppProductMaterialService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 *
 * 
 * @date Tue Jul 18 14:30:26 CST 2023
 */
@Service
public class AppProductMaterialServiceImpl extends BaseServiceImpl<AppProductMaterialEntity> implements AppProductMaterialService {

    @Resource
    private AppProductMaterialDao dao;


    @Autowired
    public void setDao(AppProductMaterialDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }


    @Override
    public List<AppProductMaterialVo> findVo(AppProductMaterialVo vo) {
        return dao.findVo(vo);
    }

    @Override
    public AppProductMaterialVo getVo(Long id) {
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(AppProductMaterialVo vo) {
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(AppProductMaterialVo vo) {
        return dao.update(vo.getEntity());
    }


    @Override
    public void downLoad(HttpServletResponse response, List<AppProductMaterialVo> list) {
        try {
            List<AppProductMaterialDown> datas = list.stream().map(item -> new AppProductMaterialDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), AppProductMaterialDown.class).sheet("模板").doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, AppProductMaterialUp.class, new UploadDataListener()).sheet().doRead();
    }


    class UploadDataListener extends AnalysisEventListener<AppProductMaterialUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(AppProductMaterialUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }


    /**内部方法**/


    /**
     * 外部方法实现
     **/
    @Override
    public List<AppProductMaterialEntity> getByProduct(Long productId) {
        if (productId == null || productId <= 0) {
            return Collections.emptyList();
        }
        AppProductMaterialEntity queryEntity = new AppProductMaterialEntity();
        queryEntity.setPid(productId);
        return dao.find(queryEntity);
    }

    @Override
    public Map<Long, Integer> getPidCountMap(List<Long> pids) {
        if (CollectionUtils.isEmpty(pids)) {
            return Collections.emptyMap();
        }
        return dao.getPidCountMap(pids).stream().collect(Collectors.toMap(CountMap::getId, CountMap::getCount));
    }

    @Override
    public List<Long> findPidList() {
        return dao.findPidList();
    }
}
